package com.consorzioimpresa.resuite.domain

import grails.plugins.springsecurity.Secured;


@Secured(['ROLE_ADMIN','ROLE_CDIMPRESA'])
class TipoTassoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [tipoTassoInstanceList: TipoTasso.list(params), tipoTassoInstanceTotal: TipoTasso.count()]
    }

    def create = {
        def tipoTassoInstance = new TipoTasso()
        tipoTassoInstance.properties = params
        return [tipoTassoInstance: tipoTassoInstance]
    }

    def save = {
        def tipoTassoInstance = new TipoTasso(params)
        if (tipoTassoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), tipoTassoInstance.id])}"
            redirect(action: "show", id: tipoTassoInstance.id)
        }
        else {
            render(view: "create", model: [tipoTassoInstance: tipoTassoInstance])
        }
    }

    def show = {
        def tipoTassoInstance = TipoTasso.get(params.id)
        if (!tipoTassoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
            redirect(action: "list")
        }
        else {
            [tipoTassoInstance: tipoTassoInstance]
        }
    }

    def edit = {
        def tipoTassoInstance = TipoTasso.get(params.id)
        if (!tipoTassoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [tipoTassoInstance: tipoTassoInstance]
        }
    }

    def update = {
        def tipoTassoInstance = TipoTasso.get(params.id)
        if (tipoTassoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (tipoTassoInstance.version > version) {
                    
                    tipoTassoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'tipoTasso.label', default: 'TipoTasso')] as Object[], "Another user has updated this TipoTasso while you were editing")
                    render(view: "edit", model: [tipoTassoInstance: tipoTassoInstance])
                    return
                }
            }
            tipoTassoInstance.properties = params
            if (!tipoTassoInstance.hasErrors() && tipoTassoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), tipoTassoInstance.id])}"
                redirect(action: "show", id: tipoTassoInstance.id)
            }
            else {
                render(view: "edit", model: [tipoTassoInstance: tipoTassoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def tipoTassoInstance = TipoTasso.get(params.id)
        if (tipoTassoInstance) {
            try {
                tipoTassoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'tipoTasso.label', default: 'TipoTasso'), params.id])}"
            redirect(action: "list")
        }
    }
}
